class AppVersion {
  final String versionCode;
  final String versionName;
  final int buildNumber;
  final String? fileName;
  final int? fileSize;
  final String? fileMd5;
  final String? downloadUrl;
  final String? updateDescription;
  final String? minSupportedVersion;
  final bool isActive;
  final bool isLatest;
  final bool forceUpdate;
  final DateTime? createdAt;
  final DateTime? updatedAt;

  AppVersion({
    required this.versionCode,
    required this.versionName,
    required this.buildNumber,
    this.fileName,
    this.fileSize,
    this.fileMd5,
    this.downloadUrl,
    this.updateDescription,
    this.minSupportedVersion,
    this.isActive = true,
    this.isLatest = false,
    this.forceUpdate = false,
    this.createdAt,
    this.updatedAt,
  });

  factory AppVersion.fromJson(Map<String, dynamic> json) {
    return AppVersion(
      versionCode: json['versionCode'] ?? '',
      versionName: json['versionName'] ?? '',
      buildNumber: json['buildNumber'] ?? 0,
      fileName: json['fileName'],
      fileSize: json['fileSize'],
      fileMd5: json['fileMd5'],
      downloadUrl: json['downloadUrl'],
      updateDescription: json['updateDescription'],
      minSupportedVersion: json['minSupportedVersion'],
      isActive: json['isActive'] ?? true,
      isLatest: json['isLatest'] ?? false,
      forceUpdate: json['forceUpdate'] ?? false,
      createdAt: json['createdAt'] != null ? DateTime.parse(json['createdAt']) : null,
      updatedAt: json['updatedAt'] != null ? DateTime.parse(json['updatedAt']) : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'versionCode': versionCode,
      'versionName': versionName,
      'buildNumber': buildNumber,
      'fileName': fileName,
      'fileSize': fileSize,
      'fileMd5': fileMd5,
      'downloadUrl': downloadUrl,
      'updateDescription': updateDescription,
      'minSupportedVersion': minSupportedVersion,
      'isActive': isActive,
      'isLatest': isLatest,
      'forceUpdate': forceUpdate,
      'createdAt': createdAt?.toIso8601String(),
      'updatedAt': updatedAt?.toIso8601String(),
    };
  }
}

class CheckUpdateRequest {
  final String deviceId;
  final String currentVersion;
  final int? buildNumber;
  final String platform;
  final String? deviceModel;
  final String? osVersion;

  CheckUpdateRequest({
    required this.deviceId,
    required this.currentVersion,
    this.buildNumber,
    required this.platform,
    this.deviceModel,
    this.osVersion,
  });

  Map<String, dynamic> toJson() {
    return {
      'deviceId': deviceId,
      'currentVersion': currentVersion,
      'buildNumber': buildNumber,
      'platform': platform,
      'deviceModel': deviceModel,
      'osVersion': osVersion,
    };
  }
}

class CheckUpdateResponse {
  final bool hasUpdate;
  final AppVersion? latestVersion;
  final bool forceUpdate;
  final String? message;

  CheckUpdateResponse({
    required this.hasUpdate,
    this.latestVersion,
    this.forceUpdate = false,
    this.message,
  });

  factory CheckUpdateResponse.fromJson(Map<String, dynamic> json) {
    return CheckUpdateResponse(
      hasUpdate: json['needUpdate'] ?? false,
      latestVersion: json['latestVersion'] != null 
          ? AppVersion.fromJson(json['latestVersion']) 
          : null,
      forceUpdate: json['updateInfo']?['isForceUpdate'] ?? false,
      message: json['message'],
    );
  }
}